package st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork.partrecord;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.MaintainWorkDetailBean;
import st.business.tepth.business.bean.PartLocationBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-07-01 14:50
 */

public class PartRecordModel implements PartRecordContract.Model{
    @Override
    public Disposable getDetailData(String token, int maintenanceId,
                                    final OnDataConvertListener<MaintainWorkDetailBean> listener) {
        return RxRestClient.builder()
                .url(Url.GetMaintainWorkDetail)
                .params("token", token)
                .params("maintenanceId", maintenanceId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertWorkDetailBean(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }
    @Override
    public Disposable postProgram(final String token, final int repairId,
                                  int repairTaskId, final int maintainId,
                                  String taskItemList,
                                  final OnDataConvertListener<MaintainWorkDetailBean> listener) {
        return RxRestClient.builder()
                .url(Url.MaintainProgramSignal)
                .params("token", token)
                .params("repairTaskId", repairTaskId)
                .params("maintenanceId", maintainId)
                .params("taskItemList", taskItemList)
                .build()
                .flatPost()
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .flatMap(new Function<String, ObservableSource<String>>() {
                    @Override
                    public ObservableSource<String> apply(String response) throws Exception {
                        JSONObject jsonObject = JSONObject.parseObject(response);
                        int ResultCode = jsonObject.getInteger("code");
                        String ResultMsg = jsonObject.getString("msg");
                        if (0 == ResultCode) {
                            return RxRestClient.builder()
                                    .url(Url.GetMaintainWorkDetail)
                                    .params("token", token)
                                    .params("maintenanceId", maintainId)
                                    .build()
                                    .onlyPost();
                        } else {
                            return Observable.error(new Throwable(ResultMsg));
                        }

                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<String>() {

                    @Override
                    public void onNext(String response) {
                        JSONObject jsonObject = JSONObject.parseObject(response);
                        int ResultCode = jsonObject.getInteger("code");
                        String object = jsonObject.getString("data");
                        String ResultMsg = jsonObject.getString("msg");
                        if (0 == ResultCode) {
                            listener.convert(convertWorkDetailBean(object));
                        } else {
                            listener.error(ResultCode, ResultMsg);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.error(-1, e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public Disposable deleteProgram(String token, int repairId, int faultDiagnosticId, int planId, final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.MaintainProgramDelete)
                .params("repairId", repairId)
                .params("token", token)
                .params("diagnosticId", faultDiagnosticId)
                .params("planId", planId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }
    @Override
    public Disposable addTempLocation(String token, String tempLocation, final OnDataConvertListener<List<PartLocationBean>> listener) {
        return RxRestClient.builder()
                .url(Url.AddTempLocation)
                .params("token", token)
                .params("positionList", tempLocation)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertPartLocation(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }
    private List<PartLocationBean> convertPartLocation(String response) {
        return new ObjConverter<List<PartLocationBean>>().convert(response
                , new TypeReference<List<PartLocationBean>>() {
                });
    }
    private MaintainWorkDetailBean convertWorkDetailBean(String response) {
        return new ObjConverter<MaintainWorkDetailBean>().convert(response
                , new TypeReference<MaintainWorkDetailBean>() {
                });
    }
}
